package com.sundy.widgetlibrary.image;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;

/**
 * Created by David on 2018/3/18.
 */

public class UEImage {
    private Bitmap bitmap=null;

    public UEImage(Drawable drawable){
        drawableToBitmap(drawable);
    }

    public UEImage(Resources res,int resID){
        this(res.getDrawable(resID));
    }

    public UEImage(Bitmap bitmap){

    }

    public UEImage(String filePath){

    }

    public UEImage(byte[] data){

    }

    public UEImage(String filePath,boolean resize){

    }

    /**
     * 将drawable转化成bitmap
     * @param drawable
     */
    private void drawableToBitmap(Drawable drawable){
        final int width=drawable.getIntrinsicWidth();
        final int height=drawable.getIntrinsicHeight();
        Bitmap bitmap=Bitmap.createBitmap(width,height,drawable.getOpacity()!= PixelFormat.OPAQUE? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565);
        Canvas canvas=new Canvas(bitmap);
        drawable.setBounds(0,0,width,height);
        drawable.draw(canvas);
        this.bitmap=bitmap;
    }

    public Bitmap toBitmap(){
        return bitmap;
    }

    /**
     * 生成图片
     * @param color 背景颜色
     * @param radius 圆角角度
     * @return
     */
    public static ShapeDrawable createBackground(int color,float radius){

        return createBackground(color,255,radius);
    }

    /**
     * 生成图片
     * @param color
     * @param alpha
     * @param radius
     * @return
     */
    public static ShapeDrawable createBackground(int color,int alpha,float radius){
        float[] outerR=new float[]{radius,radius,radius,radius,radius,radius,radius,radius};
        RoundRectShape roundRectShape=new RoundRectShape(outerR,null,null);
        ShapeDrawable shapeDrawable=new ShapeDrawable(roundRectShape);
        shapeDrawable.getPaint().setColor(color);
        shapeDrawable.getPaint().setAlpha(alpha);
        shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        return shapeDrawable;
    }
}
